//
//  HRDynamicTableViewModel.m
//  Hear
//
//  Created by mac on 2017/3/26.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRDynamicTableViewModel.h"
#import "HRDynamicCell.h"
#import "HRDunamicLayout.h"
#import "HRVideoPlayerCell.h"
#import "HRBannerCell.h"
#import "HRAnswerBannerViewController.h"
//#import "UIView+WebVideoCache.h"
//#import "UITableView+VideoPlay.h"

@interface HRDynamicTableViewModel ()
@property (nonatomic, strong)HRDynamicModel *lastDynamicModel;
@property(nonatomic, assign)CGFloat offsetY_last;
@property (nonatomic, strong)NSIndexPath  *lastPlayIndexPath;

@end
@implementation HRDynamicTableViewModel


-(NSMutableArray *)contentArr{
    if (_contentArr == nil){
        _contentArr = [NSMutableArray array];
    }
    return _contentArr;
}

- (void)handleWithTable:(UITableView *)table
{
    self.tableView = table;
    table.dataSource = self;
    table.delegate = self;
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.contentArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HRDunamicLayout *layout = self.contentArr[indexPath.row];
    if ([layout.dynamicModel.MomentType intValue] != 6){
        HRDynamicCell *cell = [HRDynamicCell cellWithTableView:tableView];
        if (layout)
        {
            cell.layout = layout;
        }
        WeakSelf
        cell.playMedioBlock = ^(HRDynamicModel *currentDynamicModel){
            if (currentDynamicModel || self.lastDynamicModel){
                dispatch_async(dispatch_get_main_queue(), ^{
                    if(![currentDynamicModel.LocalId isEqualToString:self.lastDynamicModel.LocalId]){
                        weakSelf.lastDynamicModel.isPlaying = NO;
                    }
                    [weakSelf.tableView reloadData];
                    self.lastDynamicModel = currentDynamicModel;
                });
            }
        };
        cell.deleteHistoryAnswerBlock = ^{
            if (weakSelf.deleteHistoryAnswerBlock){
                weakSelf.deleteHistoryAnswerBlock(indexPath.row);
            }
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
       
    }else if ([layout.dynamicModel.MomentType intValue] == 6){
        HRBannerCell *cell = [HRBannerCell cellWithTableView:tableView];
        cell.layout = layout;
        cell.answerButtonBlock = ^{
            HRAnswerBannerViewController *VC = [HRAnswerBannerViewController new];
            [VIEWCCONTROLER.navigationController pushViewController:VC animated:YES];
        };
        return cell;
    }
//    else{
//        HRVideoPlayerCell *cell = [HRVideoPlayerCell cellWithTableView:tableView];
//        if (layout)
//        {
//            cell.dynamicLayout = layout;
//        }
//        cell.videoPath = layout.dynamicModel.MomentDetai;
//        cell.indexPath = indexPath;
//
//
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//
//        return cell;
//    }
    return nil;

    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HRFollowDynamicLayout *layout = self.contentArr[indexPath.row];
    if ([layout.dynamicModel.MomentType intValue] == 6){
        return 420;
    }else{
        return layout.cellHeight;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HRDunamicLayout *layout = self.contentArr[indexPath.row];
    [self.delegate didSelectRowAtIndexPath:indexPath andDynamicModel:layout.dynamicModel];

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [self.delegate tableViewDynamicTableViewModelDelegate:tableView viewForHeaderInSection:section];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}



/**
 * Called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
 * 松手时已经静止, 只会调用scrollViewDidEndDragging
 */
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    
//    if (decelerate == NO)
//        // scrollView已经完全静止
//        [self.tableView handleScrollStop];
//}
//
///**
// * Called on tableView is static after finger up if the user dragged and tableView is scrolling.
// * 松手时还在运动, 先调用scrollViewDidEndDragging, 再调用scrollViewDidEndDecelerating
// */
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    
//    // scrollView已经完全静止
//    [self.tableView handleScrollStop];
//}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    // 处理滚动方向
//    [self handleScrollDerectionWithOffset:scrollView.contentOffset.y];
    
    // Handle cyclic utilization
    // 处理循环利用
//    [self.tableView handleQuickScroll];
    
//    DDLog(@"============%f",scrollView.contentOffset.y);
    
    CGFloat minAlphaOffset = - kTopHeight;
    CGFloat maxAlphaOffset = 200;
    CGFloat offset = scrollView.contentOffset.y;
    CGFloat alpha = (offset - minAlphaOffset) / (maxAlphaOffset - minAlphaOffset);
//    if (offset <= 0){
//        VIEWCCONTROLER.jz_navigationBarBackgroundAlpha = 0;
//    }else{
//        VIEWCCONTROLER.jz_navigationBarBackgroundAlpha = alpha;
//    }
    if (self.changeNavigationAlphaBlock){
        self.changeNavigationAlphaBlock(alpha);
    }
    
}

//-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    self.offsetY_last = scrollView.contentOffset.y;
//}

//-(void)handleScrollDerectionWithOffset:(CGFloat)offsetY{
//    self.tableView.currentDerection = (offsetY-self.offsetY_last>0) ? JPVideoPlayerDemoScrollDerectionUp : JPVideoPlayerDemoScrollDerectionDown;
//    self.offsetY_last = offsetY;
//}



- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if (self.contentOffsetBlock)
    {
        self.contentOffsetBlock(scrollView.contentOffset.y);
    }
}


@end
